import { BaseWindow } from "../../../component/window/BaseWindow";
import { ILayer } from "../../../mvc/interfaces/ILayer";
import { Language } from "../../../../extend/language/Language";
import { GangFightsGroupResult } from "./GangFightsGroupResult";
import { ObjCreate } from "../common/ObjCreate";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { BgItem } from "../common/BgItem";
import { SGangFightsResult } from "../../../../Message/Public/SGangFightsResult";
import { EBattleResult } from "../../../../Message/Public/EBattleResult";
import { EntityUtil } from "../../scene/layer/utils/EntityUtil";
import { GangFightsResultPlayerData } from "./data/GangFightsResultPlayerData";
import { GangFightsResultTeamData } from "./data/GangFightsResultTeamData";
import { GameMapUtil } from "../../scene/map/GameMapUtil";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { GButton } from "../../../../com/mui/controls/GButton";
type int = number;
//class GangFightsResultWindow
    
    export  class GangFightsResultWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._aryTeamResult = [];
            this._aryTeamResult2 = [];
            this._aryPlayerResult = [];
            this._aryPlayerResult2 = [];
            setSize(839, 556);
            titleHeight = 26;
            title = Language.getString(44521);
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            this.resultCell1 = new GangFightsGroupResult();
            this.resultCell1.x = 18;
            this.resultCell1.y = 38;
            this.addChild(this.resultCell1);
            this.resultCell2 = new GangFightsGroupResult();
            this.resultCell2.x = 18;
            this.resultCell2.y = 272;
            this.addChild(this.resultCell2);
            this._btnOK = ObjCreate.createGButton(Language.getString(44531), 349, 512, 70, 26, this);
            this._btnOK.addEventListener(flash.events.MouseEvent.CLICK, this.onClickOK);
            ObjCreate.createTextField(Language.getString(44576), 28, 518, 300, 20, this, GlobalStyle.textFormatGreen);
            this._tfItemReward = ObjCreate.createTextField(Language.getString(44588), 516, 518, 300, 20, this, GlobalStyle.textFormatGreen);
            this._bgItem = new BgItem();
            this._bgItem.x = 674;
            this._bgItem.y = 507;
            this.addChild(this._bgItem);
            return;
        }

        public updateResult(arg1: SGangFightsResult): void
        {
            this._result = arg1;
            this.rebuildResult();
            this.updateView();
            return;
        }

        /* internal  */rebuildResult(): void
        {
            this._aryTeamResult = [];
            this._aryPlayerResult = [];
            this._aryTeamResult2 = [];
            this._aryPlayerResult2 = [];
            this.rebuildResultGroup(this._result.groupOne, this._aryTeamResult, this._aryPlayerResult, this._result.groupOneResult);
            this.rebuildResultGroup(this._result.groupTwo, this._aryTeamResult2, this._aryPlayerResult2, this._result.groupTwoResult);
            return;
        }

        /* internal  */rebuildResultGroup(arg1: Array<any>, arg2: Array<any>, arg3: Array<any>, arg4: EBattleResult): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc6=false;
            loc3 = 0;
            while (loc3 < arg1.length) 
            {
                loc6 = false;
                loc1 = arg1[loc3];
                loc4 = 0;
                while (loc4 < arg2.length) 
                {
                    if ((loc2 = arg2[loc4]).teamName == loc1.teamName && EntityUtil.isSameServer(loc2.entityId, loc1.entityId)) 
                    {
                        var loc7;
                        var loc8=((loc7 = loc2).teamMemberNum + 1);
                        loc7.teamMemberNum = loc8;
                        loc2.teamMarkAdd = loc2.teamMarkAdd + loc1.teamMarkAdd;
                        loc2.aryPlayerResult.push(new GangFightsResultPlayerData(loc1, arg4));
                        loc6 = true;
                        break;
                    }
                    ++loc4;
                }
                if (!loc6) 
                {
                    (loc2 = new GangFightsResultTeamData()).entityId = loc1.entityId;
                    loc2.teamMark = loc1.teamMark;
                    loc2.teamMarkAdd = loc1.teamMarkAdd;
                    loc2.teamMemberNum = 1;
                    loc2.faceId = loc1.faceId;
                    loc2.teamName = loc1.teamName;
                    loc2.aryPlayerResult = [new GangFightsResultPlayerData(loc1, arg4)];
                    arg2.push(loc2);
                }
                ++loc3;
            }
            arg2.sort(this.sortByTeamNum);
            var loc5=new Array<any>();
            loc3 = 0;
            while (loc3 < arg2.length) 
            {
                loc2 = arg2[loc3];
                loc5 = loc5.concat(loc2.aryPlayerResult);
                ++loc3;
            }
            loc3 = 0;
            while (loc3 < loc5.length) 
            {
                arg3.push(loc5[loc3]);
                ++loc3;
            }
            return;
        }

        /* internal  */sortByTeamNum(arg1: GangFightsResultTeamData, arg2: GangFightsResultTeamData): int
        {
            if (arg2.teamMemberNum > arg1.teamMemberNum) 
            {
                return 1;
            }
            return -1;
        }

        /* internal  */updateView(): void
        {
            this.resultCell1.updateData(this._aryTeamResult, this._aryPlayerResult);
            this.resultCell2.updateData(this._aryTeamResult2, this._aryPlayerResult2);
            if (this._result.itemReward) 
            {
                this._bgItem.visible = true;
                this._tfItemReward.visible = true;
                this._bgItem.setItemCode(this._result.itemReward);
            }
            else 
            {
                this._bgItem.visible = false;
                this._tfItemReward.visible = false;
            }
            return;
        }

        /* internal  */onClickOK(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.hide();
            if (GameMapUtil.curMapState.isGangFights) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GangFightExitNow));
            }
            return;
        }

        public static get instance(): GangFightsResultWindow
        {
            if (!GangFightsResultWindow._instance) 
            {
                GangFightsResultWindow._instance = new GangFightsResultWindow();
            }
            return GangFightsResultWindow._instance;
        }

        public static hasInstance(): Boolean
        {
            return !(GangFightsResultWindow._instance == null);
        }

        /* internal */ /* var */_result: SGangFightsResult;

        /* internal */ /* var */_aryTeamResult: Array<any>;

        /* internal */ /* var */_aryTeamResult2: Array<any>;

        /* internal */ /* var */_aryPlayerResult: Array<any>;

        /* internal */ /* var */_aryPlayerResult2: Array<any>;

        /* internal */ /* var */resultCell1: GangFightsGroupResult;

        /* internal */ /* var */resultCell2: GangFightsGroupResult;

        /* internal */ /* var */_btnOK: GButton;

        /* internal */ /* var */_tfItemReward: Object /* flash.text.TextField */;

        /* internal */ /* var */_bgItem: BgItem;

        /* internal */ static /* var */_instance: GangFightsResultWindow;
    }
